package com.foryou.swkj.core.util;

import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GenerateVerifyCodeServlet {
    private static final String CONTENT_TYPE = "image/jpeg";
	private List<int[]> canUsedColors;

	public void init() {
		this.canUsedColors = new ArrayList();

		this.canUsedColors.add(new int[]{255, 128, 128});
		this.canUsedColors.add(new int[]{255});
		this.canUsedColors.add(new int[]{128, 64, 64});
		this.canUsedColors.add(new int[]{128});
		this.canUsedColors.add(new int[]{64});
		this.canUsedColors.add(new int[3]);
		this.canUsedColors.add(new int[]{255, 128, 64});
		this.canUsedColors.add(new int[]{255, 128});
		this.canUsedColors.add(new int[]{128, 64});
		this.canUsedColors.add(new int[]{128, 128});
		this.canUsedColors.add(new int[]{0, 255});
		this.canUsedColors.add(new int[]{0, 128});
		this.canUsedColors.add(new int[]{0, 128, 128});
		this.canUsedColors.add(new int[]{0, 128, 64});
		this.canUsedColors.add(new int[]{0, 64, 64});
		this.canUsedColors.add(new int[]{0, 64, 128});
		this.canUsedColors.add(new int[]{0, 128, 255});
		this.canUsedColors.add(new int[]{0, 128, 192});
		this.canUsedColors.add(new int[]{128, 128, 255});
		this.canUsedColors.add(new int[]{255, 128, 255});
		this.canUsedColors.add(new int[]{255, 0, 255});
		this.canUsedColors.add(new int[]{255, 0, 128});
		this.canUsedColors.add(new int[]{128, 0, 255});
		this.canUsedColors.add(new int[]{64, 0, 128});
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		init();
		String picname = StringUtils.isNotBlank(request.getAttribute("picname")+"")?request.getAttribute("picname")+"": "verifyCode";
		response.setContentType("image/jpeg");
		int iWidth = 60;
		int iHeight = 18;
		BufferedImage image = new BufferedImage(iWidth, iHeight, 1);

		Graphics g = image.getGraphics();

		g.setColor(Color.white);
		g.fillRect(0, 0, iWidth, iHeight);

		g.setColor(Color.BLACK);
		g.drawRect(0, 0, iWidth - 1, iHeight - 1);

		String rand = String.valueOf(Math.random() * 1000000.0D);
		switch (rand.length()) {
			case 1 :
				rand = "000" + rand;
				break;
			case 2 :
				rand = "00" + rand;
				break;
			case 3 :
				rand = "0" + rand;
				break;
			default :
				rand = rand.substring(0, 4);
		}

		Random random = new Random();

		request.getSession().setAttribute(picname, rand);

		for (int iIndex = 0; iIndex < 20; ++iIndex) {
			int x = random.nextInt(iWidth);
			int y = random.nextInt(iHeight);
			g.setColor(getRandColor(100, 200));
			g.drawLine(x, y, x, y);
		}

		for (int iIndex = 0; iIndex < 20; ++iIndex) {
			int x = random.nextInt(iWidth);
			int y = random.nextInt(iHeight);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.setColor(getRandColor(100, 200));
			g.drawLine(x, y, x + xl, y + yl);
		}

		g.setColor(getRandColor(0, 100));
		g.setFont(new Font("Arial", 3, 18));
		for (int i = 1; i <= 4; ++i) {
			String singleRand = rand.substring(i - 1, i);
			int[] currentColor = (int[]) this.canUsedColors.get(random.nextInt(this.canUsedColors.size()));
			try{
				g.setColor(new Color(currentColor[0], currentColor[1], currentColor[2]));
			}catch(Exception e){
			}
			g.drawString(singleRand, 13 * (i - 1) + 5, 16);
		}

		g.dispose();

		ImageIO.write(image, "JPEG", response.getOutputStream());
	}

	Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255) {
			fc = 255;
		}
		if (bc > 255) {
			bc = 255;
		}
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	public void destroy() {
	}
}